import { http, setToken,clearToken } from '@/utils'

export const login = (loginData) => {
  return async (dispatch) => {
    const res = await http.post('/authorizations', loginData)
    // console.log(res)
    const {
      data: { token },
      message,
    } = res.data
    if (message === 'OK') {
      // console.log(token)
      setToken(token)
      dispatch({
        type: 'login/setToken',
        payload: token,
      })
    }
  }
}

export const logout = () => {
  return (dispatch) => {
    // 删除token
    dispatch({ type: 'login/clearToken' })
    clearToken()
    // 删除个人信息
    dispatch({ type: 'user/clearUserInfo'})
  }
}
